package com.example.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.entity.Argue;
import com.example.blog.entity.ArgueReply;
import com.example.blog.mapper.ArgueMapper;
import com.example.blog.mapper.ArgueReplyMapper;
import com.example.blog.service.IArgueReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.common.biz.BizException;
import com.yc.common.util.Utils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 刘扬新
 * @since 2023-06-30
 */
@Service
public class ArgueReplyServiceImpl extends ServiceImpl<ArgueReplyMapper, ArgueReply> implements IArgueReplyService {

    @Resource
    ArgueMapper argueMapper;



    @Transactional   //开启事务
    @Override
    public void sumbit(ArgueReply reply) throws BizException {
        //只能评论一次 
        LambdaQueryWrapper<ArgueReply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArgueReply::getAid, reply.getAid());
        wrapper.eq(ArgueReply::getUid, reply.getUid());
        long count = this.count(wrapper);
        Utils.check(count>0, "您已经评论过了！");

        //更新正反方数量
        Argue argue = argueMapper.selectById(reply);
        if (reply.getApprove().equals("positive")){
            argue.setpNums(argue.getpNums() + 1);
        }else{
            argue.setnNums(argue.getnNums() + 1);
        }
        argueMapper.updateById(argue);

        //保存记录
        this.save(reply);
    }
}
